package com.gg.registration.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gg.registration.enums.DictEnum;

import com.gg.registration.model.user.Patient;
import com.gg.registration.user.feignClient.DictControllerClient;
import com.gg.registration.user.mapper.PatientSMapper;
import com.gg.registration.user.service.PatientService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Author：  chenjunjia
 * Date：    2022/4/21 18:59
 * WeChat：  China_JoJo_
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientSMapper, Patient> implements PatientService {

    /**
     * 写到这里突然发现了FeignClient单独建一个模块的好处了
     * 要不然每个微服务都要重新建一个FeignClient包，比较繁琐
     * 而且每个需要远程调用微服务都要重新导入关于 feign 和 LB 的依赖
     */
    @Resource
    DictControllerClient dictControllerClient;

    /**
     * 根据userid查询就诊人表，返回就诊人列表
     * @param  userId 用户id
     * @return 就诊人列表
     */
    @Override
    public List<Patient> findAll(Long userId) {
        QueryWrapper<Patient> wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        patientList.stream().forEach(item -> {
            //封装就诊人详细信息
            this.getPatientDetail(item);
        });
        return patientList;
    }

    /**
     * 根据就诊人id查询就诊人详细信息
     * @param id 就诊人id
     * @return
     */
    @Override
    public Patient getPatientId(Long id) {
        return this.getPatientDetail(baseMapper.selectById(id));
    }

    /**
     * 从数据字典查询详细信息封装到patient中
     */
    private Patient getPatientDetail(Patient patient) {
//        String certificatesTypeEncoder = "";
//        String contactsCertificatesTypeEncoder = "";
//        try {
//            certificatesTypeEncoder = URLEncoder.encode(patient.getCertificatesType(), "UTF-8");
//            contactsCertificatesTypeEncoder = URLEncoder.encode(patient.getContactsCertificatesType(), "UTF-8");
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }

        //证件类型
        String certificatesType = (String) dictControllerClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getCertificatesType()).getData();
        //联系人证件类型
        String contactsCertificatesType = (String) dictControllerClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType()).getData();
        //省
        String province = (String) dictControllerClient.getName(patient.getProvinceCode()).getData();
        //市
        String city = (String) dictControllerClient.getName(patient.getCityCode()).getData();
        //区
        String district = (String) dictControllerClient.getName(patient.getDistrictCode()).getData();
        //添加到附加信息中
        patient.getParam().put("certificatesTypeString", certificatesType);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesType);
        patient.getParam().put("provinceString", province);
        patient.getParam().put("cityString", city);
        patient.getParam().put("districtString", district);
        patient.getParam().put("fullAddress", province + city + district + patient.getAddress());
        return patient;
    }


}
